package com.pzj.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzj.dormitory.entity.DormitoryAdmin;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.mapper.DormitoryAdminMapper;
import com.pzj.dormitory.service.DormitoryAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzj.dormitory.vo.PageVO;
import com.pzj.dormitory.vo.ResultVO;
import com.pzj.dormitory.form.RuleForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@Service
public class DormitoryAdminServiceImpl extends ServiceImpl<DormitoryAdminMapper, DormitoryAdmin> implements DormitoryAdminService {

    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;

    @Override
    public ResultVO login(RuleForm ruleForm) {
        // 1.判断管理员是否存在
        QueryWrapper<DormitoryAdmin> dormitoryAdminQueryWrapper = new QueryWrapper<>();
        dormitoryAdminQueryWrapper.eq("username",ruleForm.getUsername());
        DormitoryAdmin admin = dormitoryAdminMapper.selectOne(dormitoryAdminQueryWrapper);
        ResultVO<DormitoryAdmin> resultVo = new ResultVO<>();
        if (admin == null){
            resultVo.setCode(-1);
        }else {
            // 2.判断密码是否正确
            if (!admin.getPassword().equals(ruleForm.getPassword())){
                resultVo.setCode(-2);
            }else {
                resultVo.setCode(0);
                resultVo.setData(admin);
            }
        }
        return resultVo;
    }

    @Override
    public PageVO list(Integer pageNum, Integer size) {
        Page<DormitoryAdmin> dormitoryAdminPage = new Page<>(pageNum,size);
        Page<DormitoryAdmin> page = dormitoryAdminMapper.selectPage(dormitoryAdminPage,null);
        PageVO pageVO = new PageVO();
        pageVO.setData(page.getRecords());
        pageVO.setTotal(page.getTotal());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        // 模糊查询 + 分页
        Page<DormitoryAdmin> dormitoryAdminPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        // 新建Page对象
        Page<DormitoryAdmin> resultPage = null;
        // 判断查询条件是否为空
        if (searchForm.getValue().equals(" ")){
            // 查询所有
            resultPage = dormitoryAdminMapper.selectPage(dormitoryAdminPage,null);
        }else {
            // 条件查询(模糊查询)
            QueryWrapper<DormitoryAdmin> dormitoryAdminQueryWrapper = new QueryWrapper<>();
            dormitoryAdminQueryWrapper.like(searchForm.getKey(),searchForm.getValue());
            resultPage = dormitoryAdminMapper.selectPage(dormitoryAdminPage,dormitoryAdminQueryWrapper);
        }
        // 创建 PageVO返回对象
        PageVO pageVO = new PageVO();
        // 保存分页数据
        pageVO.setData(resultPage.getRecords());
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }
}
